package com.bw.test3;

import com.alibaba.alink.operator.batch.BatchOperator;
import com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp;
import com.alibaba.alink.operator.batch.evaluation.EvalMultiClassBatchOp;
import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
import com.alibaba.alink.operator.common.evaluation.BinaryClassMetrics;
import com.alibaba.alink.operator.common.evaluation.MultiClassMetrics;
import com.alibaba.alink.pipeline.classification.NaiveBayesTextClassifier;
import org.apache.flink.types.Row;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

public class EvalMultiClassBatchOpTest {
    @Test
    public void testEvalMultiClassBatchOp() throws Exception {

        BatchOperator.setParallelism(1);

        List<Row> df_data = Arrays.asList(
                Row.of("$31$0:1.0 1:1.0 2:1.0 30:1.0", "1.0  1.0  1.0  1.0", "1"),
                Row.of("$31$0:1.0 1:1.0 2:0.0 30:1.0", "1.0  1.0  0.0  1.0", "1"),
                Row.of("$31$0:1.0 1:0.0 2:1.0 30:1.0", "1.0  0.0  1.0  1.0", "1"),
                Row.of("$31$0:1.0 1:0.0 2:1.0 30:1.0", "1.0  0.0  1.0  1.0", "1"),
                Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0  1.0  1.0  0.0", "0"),
                Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0  1.0  1.0  0.0", "0"),
                Row.of("$31$0:0.0 1:1.0 2:1.0 30:0.0", "0.0  1.0  1.0  0.0", "0")
        );
        BatchOperator <?> batchData = new MemSourceBatchOp(df_data, "sv string, dv string, label string");
        NaiveBayesTextClassifier model = new NaiveBayesTextClassifier().setVectorCol("sv").setLabelCol("label")
                .setReservedCols("sv", "label").setPredictionCol("pred").setPredictionDetailCol("pred_detail");
        BatchOperator<?> result = model.fit(batchData).transform(batchData);

        result.print();

        // 多分类评估
        MultiClassMetrics metrics = new EvalMultiClassBatchOp().setLabelCol("label").setPredictionDetailCol(
                "pred_detail").linkFrom(result).collectMetrics();

        System.out.println("Prefix0 accuracy:" + metrics.getAccuracy());
        System.out.println("Prefix1 recall:" + metrics.getRecall("1"));// todo 查一下1的意思
        System.out.println("Macro Precision:" + metrics.getMacroPrecision());
        System.out.println("Micro Recall:" + metrics.getMicroRecall());
        System.out.println("Weighted Sensitivity:" + metrics.getWeightedSensitivity());


        // 二分类评估
        BinaryClassMetrics metrics1 = new EvalBinaryClassBatchOp().setLabelCol("label").setPredictionDetailCol(
                "pred_detail").linkFrom(result).collectMetrics();
        System.out.println("AUC:" + metrics1.getAuc());
        System.out.println("KS:" + metrics1.getKs());
        System.out.println("PRC:" + metrics1.getPrc());
        System.out.println("Accuracy:" + metrics1.getAccuracy());
        System.out.println("Macro Precision:" + metrics1.getMacroPrecision());
        System.out.println("Micro Recall:" + metrics1.getMicroRecall());
        System.out.println("Weighted Sensitivity:" + metrics1.getWeightedSensitivity());



    }
}
